package com.sgb.kjwl.viewmodel.self.prichcheck;

import com.sgb.kjwl.api.APIManager;
import com.sgb.kjwl.model.entity.pricecheck.EnquirySearchCompany;
import com.sgb.kjwl.model.entity.pricecheck.SupplierEntitiy;
import com.sgb.kjwl.repository.SelfRepertory;
import com.sgb.kjwl.view.ui.activity.priceCheck.FindSupplierActivity;
import com.swgk.core.base.BaseViewModel;
import com.swgk.core.base.model.entity.BaseEntity;
import com.swgk.core.dialog.DialogHelper;
import com.swgk.core.util.MToast;

import java.util.List;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class FindSupplierModel extends BaseViewModel {
    private FindSupplierActivity activity;
    private SelfRepertory repertory;

    public FindSupplierModel(FindSupplierActivity activity, SelfRepertory repertory) {
        this.activity = activity;
        this.repertory = repertory;
    }

    /***
     * 供应商列表
     * @param name
     */
    public void getSupplier(String name) {
        EnquirySearchCompany enquirySearchCompany = new EnquirySearchCompany();
        enquirySearchCompany.setName(name);
        DialogHelper.showProgressDialog(activity, null, "数据加载中...", 0, false, null).setCanceledOnTouchOutside(false);
        APIManager.getInstance().getSelfOwnerAPI().getSupplier(enquirySearchCompany).enqueue(new Callback<BaseEntity<List<SupplierEntitiy>>>() {
            @Override
            public void onResponse(Call<BaseEntity<List<SupplierEntitiy>>> call, Response<BaseEntity<List<SupplierEntitiy>>> response) {
                DialogHelper.dismissProgressDialog();
                if (response.body() == null || response.body().getState() == null) {
                    return;
                }
                if (response.body().getState().equals(APIManager.RESPONSE_OK)) {
                    activity.fillSupplierList(response.body().getData());
                } else {
                    MToast.showToast(activity, response.body().getMsg());
                }
            }

            @Override
            public void onFailure(Call<BaseEntity<List<SupplierEntitiy>>> entityCall, Throwable throwable) {
                DialogHelper.dismissProgressDialog();
            }
        });

    }
}
